<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="./bootstrap.css">
    <style>
        .form-group {
            margin-bottom: 0;
        }
    </style>
</head>

<body>
    <div id="app">
        <div class="container">
            <div class="form-group">
                <input type="email" class="form-control"  placeholder="请输入图书名称" v-model="bookname" @keydown.enter="fn">
            </div>
            <table class="table table-bordered">
                <tr>
                    <td>id</td>
                    <td>书名</td>
                    <td>添加时间</td>
                    <td>操作</td>
                </tr>
                <tr v-for="(book, index) in books">
                    <td>{{ book.id }}</td>
                    <td>{{ book.name }}</td>
                    <td>{{ book.time }}</td>
                    <td>
                        <button type="button" class="btn btn-danger" @click="del(index)">删除</button>
                    </td>
                </tr>
            </table>
        </div>

    </div>
    <script src="./node_modules/vue/dist/vue.js"></script>
    <script>
        new Vue({
            el: '#app',
            data: {
                bookname: '',
                books: [
                    // {
                    //     id: 1,
                    //     name: '活法',
                    //     time: new Date()
                    // },
                    // {
                    //     id: 1,
                    //     name: '活法',
                    //     time: new Date()
                    // }
                ]
            },
            methods: {
                fn() {
                    if(this.bookname.trim() === '') {
                        alert('书名不能为空')
                        return
                    }
                    this.books.push({
                        id: new Date().getTime(),
                        name: this.bookname,
                        time: new Date().toDateString()
                    })
                    // 清空文本框
                    this.bookname = ''
                },
                del(index) {
                    this.books.splice(index, 1)
                }
            }
        })
    </script>
</body>

</html>